//
//  ViewController.m
//  加速计_陀螺仪_磁力计
//
//  Created by dllo on 15/11/13.
//  Copyright © 2015年 大远. All rights reserved.
//

#import "ViewController.h"
#import <CoreMotion/CoreMotion.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()<CLLocationManagerDelegate>
@property(nonatomic, strong)UILabel *CMAlabel; /**< 加速度 */
@property(nonatomic, strong)UILabel *CMGlabel; /**< 陀螺仪 */
@property(nonatomic, strong)UILabel *CMMlabel; /**< 磁力计 */
@property(nonatomic, strong)CMMotionManager *motionManager;
@property(nonatomic, strong)CLLocationManager *locationManager;
@end

@implementation ViewController {
    CALayer *znzlayer;
}

// 添加 CoreMotion.framework.

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self testTwo];

//    self.CMAlabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 50, 375, 150)];
//    self.CMAlabel.numberOfLines = 5;
//    self.CMAlabel.backgroundColor = [UIColor yellowColor];
//    [self.view addSubview:self.CMAlabel];
//    //创建CMMotionManager
//    self.motionManager = [CMMotionManager new];
//    //创建子线程来更新数据
//    NSOperationQueue *queue = [NSOperationQueue new];
//    // 判断是否可用
//    if (self.motionManager.accelerometerAvailable) {
//        //设置CMMotionManager的加速度读更新频率; 设置采样间隔
//        self.motionManager.accelerometerUpdateInterval = 0.1; // 1秒10次
//        //使用代码块开始获取加速度的数据 ,开始采样(push方法)在次队列采集完后返回主队列,返回accelerometerData
//        [self.motionManager startAccelerometerUpdatesToQueue:queue withHandler:^(CMAccelerometerData *  accelerometerData, NSError *  error) {
//            NSString *labeltext;
//            if (error) {
//                //停止获取加速度数据
//                [self.motionManager stopAccelerometerUpdates];
//                labeltext = [NSString stringWithFormat:@"获取加速度数据错误 %@",error];
//            }else{
//                //分别获取X,Y,Z轴上的加速度数据
//                labeltext = [NSString stringWithFormat:@"加速度为\n------\nX轴%+.2f \nY轴%+.2f \nZ轴%+.2f",
//                             accelerometerData.acceleration.x,
//                             accelerometerData.acceleration.y,
//                             accelerometerData.acceleration.z];
//            }
//            //在主线程更新文本
//            [self.CMAlabel performSelectorOnMainThread:@selector(setText:) withObject:labeltext waitUntilDone:NO];
////
////            注意：
////            1.当前线程为主线程的时候，waitUntilDone:YES参数无效。
////            2.该方法主要用来用主线程来修改页面UI的状态。
//        }];
//    }else{
//        NSLog(@"加速计不可用");
//    }
//    
//    
//    
//    
//    
//    self.CMGlabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 220, 375, 150)];
//    self.CMGlabel.numberOfLines = 5;
//    self.CMGlabel.backgroundColor = [UIColor yellowColor];
//    [self.view addSubview:self.CMGlabel];
//    
//    
//    //CMMotionManager 支持获取陀螺仪数据
//    if (self.motionManager.gyroAvailable) {
//        //设置获取陀螺仪数据的更新频率;
//        self.motionManager.gyroUpdateInterval = 0.1;
//        //使用代码块开始获取陀螺仪数据,开始采集在次队列采集完后返回主队列,返回accelerometerData
//        [self.motionManager startGyroUpdatesToQueue:queue withHandler:^(CMGyroData * gyroData, NSError * error) {
//            NSString *labeltext;
//            if (error) {
//                [self.motionManager stopGyroUpdates];
//                labeltext  = [NSString stringWithFormat:@"获取陀螺仪数据出现错误%@",error];
//                
//            }else{
//                labeltext = [NSString stringWithFormat:@"绕各轴转速为\n------------------\nX轴%+.2f \nY轴%+.2f \nZ轴%+.2f",
//                             gyroData.rotationRate.x,
//                             gyroData.rotationRate.y,
//                             gyroData.rotationRate.z];
//                             
//                             
//            }
//            [self.CMGlabel performSelectorOnMainThread:@selector(setText:) withObject:labeltext waitUntilDone:NO];
//            
//            
//            
//        }];
//    }else{
//        NSLog(@"该设备不支持获取陀螺仪数据");
//    }
//    
//    
//    
//    
//    // 磁力计
//    self.CMMlabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 390, 375, 150)];
//    [self.view addSubview:self.CMMlabel];
//    self.CMMlabel.numberOfLines = 5;
//    self.CMMlabel.backgroundColor = [UIColor yellowColor];
//    if (self.motionManager.magnetometerAvailable) {
//        self.motionManager.magnetometerUpdateInterval = 0.1;
//        
//        [self.motionManager startMagnetometerUpdatesToQueue:queue withHandler:^(CMMagnetometerData *  magnetometerData, NSError *  error) {
//            NSString *labeltext;
//            if (error) {
//                [self.motionManager stopMagnetometerUpdates];
//                labeltext = [NSString stringWithFormat:@"获取磁场数据出现错误%@",error];
//            }else{
//                labeltext = [NSString stringWithFormat:@"磁场数据\n------------------\nX轴%+.2f \nY轴%+.2f \nZ轴%+.2f",
//                             magnetometerData.magneticField.x,
//                             magnetometerData.magneticField.y,
//                             magnetometerData.magneticField.z];
//            }
//            
//            [self.CMMlabel performSelectorOnMainThread:@selector(setText:) withObject:labeltext waitUntilDone:NO];
//            
//        }];
//    }else{
//        NSLog(@"该设备不支持获取磁场数据");
//    }
    
    
    
    
    
    
    
    
}

- (void)testTwo {
    if ([CLLocationManager headingAvailable]) {
        znzlayer = [CALayer new];
        NSInteger screenHeight = [UIScreen mainScreen].bounds.size.height;
        NSInteger y = (screenHeight - 320) / 2;
        znzlayer.frame = CGRectMake(0, y, 320, 320);
        znzlayer.contents = (__bridge id)([(id)[UIImage imageNamed:@"zna.jpg"] CGImage]);
        [self.view.layer addSublayer:znzlayer];
        
        self.locationManager = [CLLocationManager new];
        self.locationManager.delegate = self;
        
        [self.locationManager startUpdatingHeading];
    } else {
        [[[UIAlertView alloc] initWithTitle:@"" message:@"" delegate:self cancelButtonTitle:@"" otherButtonTitles:nil, nil] show];
    }
}

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
    //
    CGFloat headings = -1.0f *M_PI *newHeading.magneticHeading / 180.0f;
    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform"];
    CATransform3D fromValue = znzlayer.transform;
    
    //
    anim.fromValue = [NSValue valueWithCATransform3D:fromValue];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
